package com.slg.android.manyanime;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.slg.android.manyanime.adapter.CommentFragmentAdapter;
import com.slg.android.manyanime.bean.Comment;
import com.slg.android.manyanime.bean.User;
import com.slg.android.manyanime.utils.DataStore;

import java.util.ArrayList;
import java.util.List;


public class CommentActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    @ViewInject(R.id.comment_navigation_radioGroup)
    private RadioGroup navigationBar;
    @ViewInject(R.id.comment_viewPager)
    private ViewPager viewPager;
    @ViewInject(R.id.common_loading)
    private RelativeLayout loading;
    @ViewInject(R.id.common_loading_image)
    private ImageView loadingImage;
    @ViewInject(R.id.common_title_title_textView)
    private TextView title;
    @ViewInject(R.id.common_title_function_textView)
    private TextView function;
    private Animation animation;
    private String lastComment;
    private String hotComment;
    private CommentFragmentAdapter adapter;
    private HttpUtils httpUtils;
    private String type;
    private String mid;
    private String uid = "";
    private boolean flag = false;
    private CallBack callBack;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_comment);
        ViewUtils.inject(this);
        title.setText("评论");
        function.setVisibility(View.VISIBLE);
        function.setText("吐槽");
        Intent intent = getIntent();
        callBack = new CallBack();
        type = intent.getStringExtra("type");
        mid = intent.getStringExtra("mid");
        user = DataStore.getDataStore().getUser();
        if (user != null) {
            uid = user.getId();
        }
        navigationBar.setOnCheckedChangeListener(this);
        viewPager.setOnPageChangeListener(this);
        animation = AnimationUtils.loadAnimation(this, R.anim.anim_common_rotate);
        //开启加载中动画
        loading.setVisibility(View.VISIBLE);
        loadingImage.startAnimation(animation);

        //开启网络加载评论内容
        httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET,
                "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=%E7%99%BE%E5%BA%A6&m=Index&a=commentList&uid=" + uid + "&last_id=&mid=" + mid + "&module=" + type + "&order=new&login_uid=" + uid
                , callBack);

    }

    @OnClick({R.id.common_title_function_textView, R.id.common_title_back_imageView})
    public void OnClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.common_title_back_imageView:
                finish();
                break;
            case R.id.common_title_function_textView:
                //TODO 跳转至发表评论
                if (user != null) {
                    Intent intent = new Intent(this, IssueActivity.class);
                    intent.putExtra("mid", mid);
                    startActivity(intent);
                } else {

                }
                break;
        }
    }

    private class CallBack extends RequestCallBack<String> {

        @Override
        public void onSuccess(ResponseInfo<String> stringResponseInfo) {
            if (!flag) {
                flag = true;
                lastComment = stringResponseInfo.result;
                httpUtils.send(HttpRequest.HttpMethod.GET
                        , "http://manhua.api.61.com/?&pf=1&android_version=16&app_version=512&app_channel=%E7%99%BE%E5%BA%A6&m=Index&a=commentList&uid=" + uid + "&last_id=&mid=" + mid + "&module=" + type + "&order=hot&login_uid=" + uid
                        , callBack);

            } else {
                hotComment = stringResponseInfo.result;
                adapter = new CommentFragmentAdapter(getSupportFragmentManager()
                        , lastComment, hotComment, mid, type);
                viewPager.setAdapter(adapter);
                //停止加载动画
                animation.cancel();
                loading.setVisibility(View.GONE);
            }

        }

        @Override
        public void onFailure(HttpException e, String s) {

        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton child = (RadioButton) group.getChildAt(i);
            if (child.isChecked()) {
                //Log.i("checked","pos"+i);
                child.setTextColor(Color.WHITE);
                child.setBackgroundDrawable(
                        getResources().getDrawable(R.drawable.navigation_bar_item_checked_bg));
                viewPager.setCurrentItem(i);
            } else {
                child.setTextColor(getResources().getColor(R.color.main_menu_text));
                child.setBackgroundDrawable(
                        getResources().getDrawable(R.drawable.navigation_bar_item_bg));
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (navigationBar != null) {
            RadioButton child = (RadioButton) navigationBar.getChildAt(position);
            child.setChecked(true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
